#!/usr/bin/perl -w

#=======================================================================
# std
# File ID: e9801250-f743-11dd-afdc-000475e441b9
# Send file templates to stdout
#
# Character set: UTF-8
# ©opyleft 2004– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;
use Cwd;

$| = 1;

our $Debug = 0;

our %Opt = (

    'debug' => 0,
    'force' => 0,
    'help' => 0,
    'local' => 0,
    'no-svn' => 0,
    'notag' => "",
    'tag' => 0,
    'verbose' => 0,
    'version' => 0,

);
my @Tags = ();

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";

my $CMD_GIT = "git";
my $CMD_SVN = "svn";
my $CMD_WGET = "wget";
my $CMD_SUUID = "suuid";

my $Type = "";
my $Dest = "";
my $session_uuid = "";
my @bck_cmdline = @ARGV;

Getopt::Long::Configure("bundling");
GetOptions(

    "debug" => \$Opt{'debug'},
    "force|f" => \$Opt{'force'},
    "help|h" => \$Opt{'help'},
    "local|l+" => \$Opt{'local'},
    "no-svn|n" => \$Opt{'no-svn'},
    "notag|T=s" => \$Opt{'notag'},
    "tag|t=s" => \@Tags,
    "verbose|v+" => \$Opt{'verbose'},
    "version" => \$Opt{'version'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

my $Lh = "[0-9a-fA-F]";
my $v1_templ = "$Lh\{8}-$Lh\{4}-1$Lh\{3}-$Lh\{4}-$Lh\{12}";

if (defined($ARGV[0])) {
    $Type = $ARGV[0];
} else {
    usage(1);
}

defined($ARGV[1]) && ($Dest = $ARGV[1]);

my %Tag = ();
if (scalar(@Tags)) {
    for my $Curr (@Tags) {
        if ($Curr =~ /^([^=]+?)=(.*)$/) {
            $Tag{$1} = $2;
        } else {
            die("$progname: $Curr: Invalid tag, must have the form name=value\n");
        }
    }
}

if (length($Dest) && -e $Dest) {
    if ($Opt{'force'}) {
        msg(1, "Overwriting '$Dest'...");
    } else {
        warn("$progname: $Dest: File already exists, will not overwrite\n");
        exit(1);
    }
}

D(sprintf("\@Tags = \"%s", join("\", \"", @Tags) . "\""));
D(sprintf("%%Tag = \"%s", join("\", \"", %Tag) . "\""));

my $dat_dir = $0;
$dat_dir =~ s/^(.+\/)(.+?)$/$1/;
$dat_dir .= "Lib/std";
my $dat_file = "$dat_dir/$Type";

my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday, $is_dst) = localtime();
$Year += 1900;
$Mon += 1;

my $URL = "https://github.com/sunny256/utils/raw/master/Lib/std/$Type";
my $tmpfile = "tmp.std." . time;

my $is_svn_wc = (!$Opt{'no-svn'} && -d ".svn/.") ? 1 : 0;

if ($Opt{'local'}) {
    my $orig_dir = getcwd();
    chdir($dat_dir) || die("$progname: $dat_dir: Cannot chdir: $!\n");
    if (open(PipeFP, "$CMD_GIT status $Type |")) {
        while (<PipeFP>) {
            if (/modified:\s+$dat_file/) {
                warn("$progname: WARNING: $dat_file is modified\n");
                last;
            }
        }
        close(PipeFP);
        chdir($orig_dir) || die("$progname: $orig_dir: Cannot return to original dir: $!\n");
        mysyst("cp", "-a", $dat_file, $tmpfile);
    } else {
        warn("$progname: Unable to open \"$CMD_GIT stat\" pipe\n");
    }
} else {
    # FIXME: Have to add --no-check-certificate to get around bug in wget(1) described at http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=409938
    mysyst($CMD_WGET, "--no-check-certificate", "-O", $tmpfile, "-o", "/dev/null", $URL);
}

if (length($Dest)) {
    msg(1, "Copying temp file");
    mysyst("cp", "-a", $tmpfile, $Dest);
}

my $Output = "";

if (open(FromFP, "<$tmpfile")) {
    while (my $Line = <FromFP>) {
        $Line = replace_tags($Line);
        $Output .= $Line;
    }
    close(FromFP);
    if ($Output =~ /STD\S+DTS/s) {
        my ($tmp, $tmp2) = ($Output, "");
        my %Ha = ();
        $tmp =~ s/STD(\S+?)DTS/($Ha{$1} = 1)/gse;
        for my $key (sort keys %Ha) {
            defined($Ha{$key}) && ($tmp2 .= "$key ");
        }
        $tmp2 =~ s/ $//;
        warn("$progname: Warning: Undefined tags: $tmp2\n");
    }
} else {
    die("$progname: $tmpfile: Cannot read file: $!\n");
}

if (length($Dest)) {
    open(DestFP, ">$Dest")
        || die("$progname: $Dest: Cannot open file for write: $!\n");
    print(DestFP $Output);
    close(DestFP);
    if ($is_svn_wc) {
        if (!is_added($Dest)) {
            msg(1, "$CMD_SVN add");
            mysyst($CMD_SVN, "add", $Dest);
        }
        msg(1, "mergesvn");
        if (-f "Lib/std/$Type") {
            mysyst("mergesvn", "-s", "Lib/std/$Type", $Dest);
        } else {
            mysyst("mergesvn", "-s", $URL, $Dest);
        }
        mysyst("keyw", $Dest);
    }
} else {
    print($Output);
}

unlink($tmpfile) || warn("$progname: $tmpfile: Cannot delete file: $!\n");

sub is_added {
    # Check if a file is already added with svn(1) {{{
    my $File = shift;
    my $Res = `$CMD_SVN stat --xml "$File"`;
    my $Retval = $Res =~ /<wc-status.*?item="added".*?<\/wc-status>/s
        ? 1
        : 0;
    return($Retval);
    # }}}
} # is_added()

sub replace_tags {
    # {{{
    my $Txt = shift;
    if (scalar(@Tags)) {
        for my $Curr (keys %Tag) {
            $Opt{'notag'} =~ /\b$Curr\b/ || $Txt =~ s/STD${Curr}DTS/$Tag{$Curr}/g;
        }
    }
    $Opt{'notag'} =~ /\bfilename\b/ || (length($Dest) && ($Txt =~ s/STDfilenameDTS/$Dest/g));
    $Opt{'notag'} =~ /\byear\b/ || ($Txt =~ s/STDyearDTS/$Year/g);
    $Opt{'notag'} =~ /\buuid\b/ || ($Txt =~ s/STDuuidDTS/gen_uuid()/ge);
    return($Txt);
    # }}}
} # replace_tags()

sub mysyst {
    # Customised system() {{{
    my @Args = @_;
    my $system_txt = sprintf("system(\"%s\");", join("\", \"", @Args));
    D("$system_txt");
    deb_wait();
    msg(1, "@_\n");
    system(@_);
    # }}}
} # mysyst()

sub gen_uuid {
    # {{{
    my $Retval = "";
    my $Comment = "$progname " . join(" ", @bck_cmdline) . "";
    if (!length($session_uuid)) {
        chomp($session_uuid = `$CMD_SUUID --tag std --whereto o --comment "$Comment"`);
        if (!defined($session_uuid) || $session_uuid !~ /^$v1_templ$/) {
            die("$progname: $CMD_SUUID error\n");
        }
        if (length($Dest)) {
            print("$session_uuid\n");
        }
    }
    $Retval = $session_uuid;
    D("gen_uuid(): Retval = '$Retval'");
    return($Retval);
    # }}}
} # gen_uuid()

sub deb_wait {
    # Wait until Enter is pressed if $Debug and verbose >= 2 {{{
    $Debug || return;
    if ($Opt{'verbose'} >= 2) {
        print("debug: Press ENTER...");
        <STDIN>;
    }
    # }}}
} # deb_wait()

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<END);

Usage: $progname [options] type [destfile]

Create a file of a specific type using a predefined template. If no 
destination file is specified, output goes to stdout.

Options:

  -h, --help
    Show this help.
  -f, --force
    Overwrite existing file.
  -l, --local
    Use local copy, don’t download.
  -n, --no-svn
    Don’t version control the file with Subversion.
  -t x, --tag x
    Replaces strings of the form "STDtagDTS" in the template file with 
    another string. Examples:
      -t testcmd=myscript.pl
      -t year=1997 -t "personname=John Lennon" -t os=GNU/Linux
    The option can be specified many times.
  -T x, --notag x
    Comma-separated list of tags not to expand. Examples:
      -T year,uuid
        Will not expand the year and uuid tags.
  -v, --verbose
    Increase level of verbosity. Can be repeated.
  --version
    Print version information.
  --debug
    Print debugging messages.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return("");
    # }}}
} # D()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME

std

=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION

Send a template of a special file type to stdout.

=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS

Quite primitive, but it works.

=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
